package com.wrox.site;

import org.apache.logging.log4j.ThreadContext;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.UUID;

public class LoggingFilter implements Filter
{
    @Override
    public void doFilter(ServletRequest request, ServletResponse response,
                         FilterChain chain) throws IOException, ServletException
    {
        ThreadContext.put("id", UUID.randomUUID().toString());
        HttpSession session = ((HttpServletRequest)request).getSession(false);
        if(session != null)
            ThreadContext.put("username",
                    (String) session.getAttribute("username"));

        try
        {
            chain.doFilter(request, response);
        }
        finally
        {
            ThreadContext.clearAll();
        }
    }

    @Override
    public void init(FilterConfig filterConfig) throws ServletException
    {

    }

    @Override
    public void destroy()
    {

    }
}
